<?php

namespace app\common\model\User;

use think\Model;

//引入软删除
use traits\model\SoftDelete;

class User extends Model
{
    use SoftDelete;

    //设置当前模型对应的完整数据表名称
    protected $name = 'business';

    //开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    //定义时间戳字段名
    protected $createTime = 'createtime';

    //关闭自动更新时间戳
    protected $updateTime = false;

    //定义软删除字段
    protected $deleteTime = 'deletetime';

    //开启过滤
    protected $field = true;

    //隐藏输出字段，存在，只不过不显示
    protected $hidden = [
        'password',
        'salt',
    ];

    //设置获取器，需要进行操作的字段
    protected $append = [
        'cleatetime_text', //创建时间
        'deletetime_text', //软删除时间
        'avatar_url', //头像地址
        'birthday_text', //生日
        // 'mobile_text', //手机号
        'gender_text', //性别
        'region_text', //地区
        'province_text', //省
        'city_text', //市
        'district_text', //区
        'school_text', //学校
    ];

    // 学校
    public function getSchoolTextAttr($value, $data)
    {
        $schoolid = isset($data['schoolid'])? $data['schoolid'] : '';
        if ($schoolid) {
            return $school_text = model('common/School/School')::where('id', $schoolid)->value('name');
        } else {
            return '';
        }   
    }

    //创建时间
    public function getCleatetimeTextAttr($value, $data)
    {
        $createtime = isset($data['createtime']) ? $data['createtime'] : 0;
        return date('Y-m-d H:i', $createtime);
    }

    //软删除时间
    public function getDeletetimeTextAttr($value, $data)
    {
        $deletetime = isset($data['deletetime']) ? $data['deletetime'] : 0;
        return date('Y-m-d H:i', $deletetime);
    }

    //头像地址
    public function getAvatarUrlAttr($value, $data)
    {
        $avatar = $data['avatar'] ?: '';

        if (empty($avatar) || !is_file('.' . $avatar)) {
            $avatar = '/assets/img/avatar.png';
        }

        // 返回一个带有域名的图片地址
        return request()->domain() . $avatar;
    }

    //生日
    public function getBirthdayTextAttr($value, $data)
    {
        $birthday = isset($data['birthday']) ? $data['birthday'] : '';

        if (empty($birthday)) {
            return '';
        }

        return date('Y-m-d', $birthday);
    }

    //性别
    public function getGenderTextAttr($value, $data)
    {
        $gender = isset($data['gender']) ? $data['gender'] : '';

        $item = [
            '0' => '保密',
            '1' => '男',
            '2' => '女',
        ];

        return $item[$gender];
    }

    //地区
    public function getRegionTextAttr($value, $data)
    {
        $region = '';

        // 省
        $province = empty($data['province']) ? '' : $data['province'];
        if ($province) {
            $province_text = model('common/Region')::where('code', $province)->value('name');
            $region = $province_text;
        }

        // 市
        $city = empty($data['city']) ? '' : $data['city'];
        if ($city) {
            $city_text = model('common/Region')::where('code', $city)->value('name');
            $region .=  "/" . $city_text;
        }

        // 区
        $district = empty($data['district']) ? '' : $data['district'];
        if ($district) {
            $district_text = model('common/Region')::where('code', $district)->value('name');
            $region .= "/" . $district_text;
        }

        //广东省/广州市/海珠区
        return $region;
    }

    //省
    public function getProvinceTextAttr($value, $data)
    {
        $province = isset($data['province']) ? $data['province'] : '';
        if ($province) {
            return $province_text = model('common/Region')::where('code', $province)->value('name');
        } else {
            return '';
        }
    }

    //市
    public function getCityTextAttr($value, $data)
    {
        $city = isset($data['city']) ? $data['city'] : '';
        if ($city) {
            return $city_text = model('common/Region')::where('code', $city)->value('name');
        } else {
            return '';
        }
    }

    //区
    public function getDistrictTextAttr($value, $data)
    {
        $district = isset($data['district']) ? $data['district'] : '';
        if ($district) {
            return $district_text = model('common/Region')::where('code', $district)->value('name');
        } else {
            return '';
        }
    }

    //连表查询
    //左联校园表
    public function school()
    {
        return $this->belongsTo('app\common\model\School\School', 'schoolid', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
